package com.lechi.web.job;

import com.lechi.web.common.utils.spring.SpringUtils;
import com.lechi.web.service.IIotMonitorDataService;
import com.lechi.web.system.domain.IotMonitorDataBo;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Collectors;

/**
 * The type Batch insert job.
 *
 * @author yangguang
 */
@Slf4j
public class BatchInsertJob implements Job {

    /**
     * The constant workQueue.
     */
    public static final LinkedBlockingQueue<IotMonitorDataBo> workQueue = new LinkedBlockingQueue<>();

    @Override
    public void execute(JobExecutionContext context) {
        if (workQueue.isEmpty()) {
            return;
        }
        Map<String, List<IotMonitorDataBo>> groupingByTableName = workQueue.stream().collect(Collectors.groupingBy(IotMonitorDataBo::getTableName));
        groupingByTableName.forEach((tableName, dataBoList) -> {
            SpringUtils.getBean(IIotMonitorDataService.class).batchInsert(tableName, dataBoList);
        });
        workQueue.clear();
    }

}